package com.dingreading.cloud.soms.service.impl;

import com.dingreading.cloud.common.util.PageUtil;
import com.dingreading.cloud.common.util.QueryFiledUtil;
import com.dingreading.cloud.soms.dto.CmorderApplyDto;
import com.dingreading.cloud.soms.entity.CmorderApply;
import com.dingreading.cloud.soms.entity.table.CmorderApplyTableDef;
import com.dingreading.cloud.soms.mapper.CmorderApplyMapper;
import com.dingreading.cloud.soms.service.CmorderApplyService;
import com.mybatisflex.core.paginate.Page;
import com.mybatisflex.core.query.QueryCondition;
import com.mybatisflex.core.query.QueryWrapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import static com.dingreading.cloud.soms.entity.table.MembersTableDef.members;

/**
 * 服务层实现。
 *
 * @author Administrator
 * @since 2024-09-11
 */
@Service
public class CmorderApplyServiceImpl extends BaseServiceImpl<CmorderApplyMapper, CmorderApply, CmorderApplyTableDef> implements CmorderApplyService {

    @Override
    protected CmorderApplyTableDef getTable() {
        return CmorderApplyTableDef.cmorderApply;
    }

    @Override
    public Page<CmorderApplyDto> pageList(PageUtil pageUtil, String corpUid, String storeUid, Integer type, String beginDate, String endDate, String keyword) {
        QueryCondition condition = QueryCondition.createEmpty();
        condition.and(table.corpUid.eq(corpUid))
                .and(table.storeUid.eq(storeUid));
        if (StringUtils.isNotBlank(beginDate) && StringUtils.isNotBlank(endDate))
            condition.and(table.addTime.between(QueryFiledUtil.appendMin00(beginDate), QueryFiledUtil.appendMin24(endDate)));
        if (null != type)
            condition.and(table.type.eq(type));
        if (StringUtils.isNotBlank(keyword))
            condition.and(members.memberName.like(keyword)
                    .or(table.applyCode.like(keyword))
                    .or(table.orderCode.like(keyword))
            );

        QueryWrapper wrapper = QueryWrapper.create()
                .select(table.allColumns, members.memberName.as("memberName"))
                .from(table)
                .leftJoin(members).on(table.memberUid.eq(members.uid))
                .where(condition)
                .orderBy(table.id.desc());
        return getMapper().paginateAs(Page.of(pageUtil.getPage(), pageUtil.getRows()), wrapper, CmorderApplyDto.class);
    }
}
